Skip to content

Method: resolveCollectionValue(String, List)

1: /**
2: * Copyright (C) 2017 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.deserialization;
16:
17: import cz.cvut.kbss.jsonld.ConfigParam;
18: import cz.cvut.kbss.jsonld.Configuration;
19: import cz.cvut.kbss.jsonld.JsonLd;
20: import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
21: import cz.cvut.kbss.jsonld.exception.UnknownPropertyException;
22:
23: import java.util.Collections;
24: import java.util.List;
25: import java.util.Map;
26:
27: public class ExpandedJsonLdDeserializer extends JsonLdDeserializer {
28:
29: private InstanceBuilder instanceBuilder;
30:
31: ExpandedJsonLdDeserializer() {
32: }
33:
34: ExpandedJsonLdDeserializer(Configuration configuration) {
35: super(configuration);
36: }
37:
38: @Override
39: public <T> T deserialize(Object jsonLd, Class<T> resultClass) {
40: if (!(jsonLd instanceof List)) {
41: throw new JsonLdDeserializationException(
42: "Expanded JSON-LD deserializer requires a JSON-LD array as input.");
43: }
44: final List<?> input = (List<?>) jsonLd;
45: assert input.size() == 1;
46: final Map<?, ?> root = (Map<?, ?>) input.get(0);
47: this.instanceBuilder = new DefaultInstanceBuilder(classResolver);
48: final Class<? extends T> targetClass = resolveTargetClass(root, resultClass);
49: instanceBuilder.openObject(targetClass);
50: processObject(root);
51: instanceBuilder.closeObject();
52: assert resultClass.isAssignableFrom(instanceBuilder.getCurrentRoot().getClass());
53: return targetClass.cast(instanceBuilder.getCurrentRoot());
54: }
55:
56: @Override
57: protected List<String> getObjectTypes(Object jsonLdObject) {
58: assert jsonLdObject instanceof Map;
59: final Object types = ((Map<?, ?>) jsonLdObject).get(JsonLd.TYPE);
60: if (types == null) {
61: return Collections.emptyList();
62: }
63: assert types instanceof List;
64: return (List<String>) types;
65: }
66:
67: private void processObject(Map<?, ?> root) {
68: for (Map.Entry<?, ?> e : root.entrySet()) {
69: final String property = e.getKey().toString();
70: final boolean shouldSkip = shouldSkipProperty(property);
71: if (shouldSkip) {
72: continue;
73: }
74: if (e.getValue() instanceof List) {
75: resolveCollectionValue(property, (List<?>) e.getValue());
76: } else {
77: // Presumably @id
78: instanceBuilder.addValue(property, e.getValue());
79: }
80: }
81: }
82:
83: private boolean shouldSkipProperty(String property) {
84: if (!instanceBuilder.isPropertyMapped(property)) {
85: if (configuration().is(ConfigParam.IGNORE_UNKNOWN_PROPERTIES)) {
86: return true;
87: }
88: throw UnknownPropertyException.create(property, instanceBuilder.getCurrentContextType());
89: }
90: return false;
91: }
92:
93: private void resolveCollectionValue(String property, List<?> value) {
94:• if (value.size() == 1 && value.get(0) instanceof Map && !instanceBuilder.isPlural(property)) {
95: resolvePropertyValue(property, (Map<?, ?>) value.get(0));
96: } else {
97: instanceBuilder.openCollection(property);
98:• for (Object item : value) {
99:• if (item instanceof Map) {
100: resolveValue((Map<?, ?>) item);
101: } else {
102: instanceBuilder.addValue(item);
103: }
104: }
105: instanceBuilder.closeCollection();
106: }
107: }
108:
109: private void resolveValue(Map<?, ?> value) {
110: if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
111: instanceBuilder.addValue(value.get(JsonLd.VALUE));
112: } else if (value.size() == 1 && value.containsKey(JsonLd.ID)) {
113: instanceBuilder.addNodeReference(value.get(JsonLd.ID).toString());
114: } else {
115: final Class<?> elementType = instanceBuilder.getCurrentCollectionElementType();
116: final Class<?> targetClass = resolveTargetClass(value, elementType);
117: assert elementType.isAssignableFrom(targetClass);
118: instanceBuilder.openObject(targetClass);
119: processObject(value);
120: instanceBuilder.closeObject();
121: }
122: }
123:
124: private void resolvePropertyValue(String property, Map<?, ?> value) {
125: if (value.size() == 1 && value.containsKey(JsonLd.VALUE)) {
126: instanceBuilder.addValue(property, value.get(JsonLd.VALUE));
127: } else if (value.size() == 1 && value.containsKey(JsonLd.ID)) {
128: instanceBuilder.addNodeReference(property, value.get(JsonLd.ID).toString());
129: } else {
130: instanceBuilder.openObject(property, getObjectTypes(value));
131: processObject(value);
132: instanceBuilder.closeObject();
133: }
134: }
135: }